import LXCOM from '../common/common.js'
import ActPkg from '../graph/action/rectaction.js'
import Coord from '../common/struct/coord.js'
import rectgraph from './rectgraph.js'

import {LGXEnums}  from '../common/sysenum.js'

import globalConst from '../common/globalConst.js'
var global = globalConst.GlobalConst;
import UtilFuncs from '../common/tools/utilfuncs.js'
var funcs = UtilFuncs.UtilFuncs;

import { CoordTRFUtil } from '../common/tools/coordtool.js'
import {FillUtil} from '../graph/paintutil/fillutil.js'
import {Strokeutil} from '../graph/paintutil/strokeutil.js'

class CombineGraph extends rectgraph.RectGraph {
    constructor(type) {
        super(type);
        var p = new Coord.MultiPos();
        this.setPos(p);
        var action = new ActPkg.RectGraphAction(this);
        this.setActionSvc(action);
        this.m_graphType = LGXEnums.LGXGraphType.Combine_type;
        this._lineDef.linemode = LGXEnums.LGXLineMode.Dash_Dash;
        this.curentChildGraph = null;
    }

    display(map, mapInfo,forClip) {
        console.log("combine graph display --------");

        if (this._fillDef.fillStyle > LGXEnums.LGXFillStyle.EMPTY) {
            this.fillCombineRegion(map, mapInfo);
        }
        else if (this._selected) {
            super.display(map, mapInfo,forClip);
        }
    }

    fillCombineRegion(map, mapInfo) {
        var lenth = this.m_tChildGraphLst.length;
        if (lenth != 2) {
            return;
        }

        var scaling = mapInfo.getScaling();
        let childGraph1 = this.m_tChildGraphLst[0];

        let position1 = childGraph1.getPos();
        var disRadius1 = scaling * position1.radius;
        var disLongRadius1 = scaling * position1.xRadius;
        var x = position1.x;
        var y = position1.y;
        var cntpt = { x, y };
        var p1 = CoordTRFUtil.world2Screen(cntpt, mapInfo);

        map.beginPath();
        map.ellipse(p1.x, p1.y, disLongRadius1, disRadius1, 0, 0, Math.PI * 2, false);
        for (let i = 1; i < lenth; i++) {
            let childGraph2 = this.m_tChildGraphLst[i];
            let position2 = childGraph2.getPos();
            var disRadius2 = scaling * position2.radius;
            var disLongRadius2 = scaling * position2.xRadius;
            x = position2.x;
            y = position2.y;
            cntpt = { x, y };
            var p2 = CoordTRFUtil.world2Screen(cntpt, mapInfo);
            map.ellipse(p2.x, p2.y, disLongRadius2, disRadius2, 0, 0, Math.PI * 2, true);
        }
        map.closePath();
        Strokeutil.active(this._lineDef, this, map, mapInfo);
        FillUtil.active(this._fillDef, map, this, mapInfo, false);
    }

    calScope(mapscale) {
        super.calScope(mapscale);
    }

    distance(tPos, scaling, mapscale) {
        var position = this.getPos();
        var lenth = position.posList.length;
        if (lenth < 2) {
            return -1;
        }
        if (!super.preCheckInScope(tPos, scaling)) {
            return -1;
        }

        var cmpGap = super.getSelCmpGap(scaling, mapscale);
        var dis = -1;
        var pCntPos = null;
        var pPrePos = position.posList[0];
        for (var i = 0; i < lenth - 1; i++) {
            pCntPos = position.posList[i + 1];
            var lLen = funcs.distanceLineSec4Visual(tPos, pPrePos, pCntPos, scaling);
            if ((lLen < dis) || (dis < 0)) {
                dis = lLen;
            }
            if (dis <= cmpGap) {
                return 0;
            }
            pPrePos = pCntPos;
        }
        if (dis > cmpGap) {
            dis = -1;
        }
        return dis;
    }


    calculateSubKind(tPoint, scaling, mapscale) {
        let seleKind = 5;
        if (!super.preCheckInScope(tPoint, scaling)) {
            seleKind = 0;
        }
        return seleKind;
    }

    calculateSubControlPoint(tPoint, scaling, mapscale) {
        let seleKind = 0;
        var lenth = this.m_tChildGraphLst.length;
        for (var i = 0; i < lenth; i++) {
            let childGraph = this.m_tChildGraphLst[i];
            seleKind = childGraph.calculateSubControlPoint(tPoint, scaling, mapscale);
            if (seleKind > 0) {
                this.curentChildGraph = childGraph;
                childGraph.setSelKind(seleKind);
                break;
            }
        }
        return seleKind;
    }


    movePosOffset(tOffset, modCmdInfo) {
        if (this._selKind < 1) {
            return;
        }

        let modifyChildFlag = false;
        if (this.curentChildGraph) {
            let kind = this.curentChildGraph.getSelKind();
            if (kind == this._selKind) {
                modifyChildFlag = true;
            }
        }

        if (modifyChildFlag) {
            this.curentChildGraph.movePosOffset(tOffset, modCmdInfo);
        }
        else {
            var position = this.getPos();
            var lenth = position.posList.length;
            if (this._selKind > lenth) {
                this.translate(tOffset, modCmdInfo.mapscale,false);
            }
        }
    }

    setCurentChildGraph(p) { this.curentChildGraph = p; }
    getCurentChildGraph() { return this.curentChildGraph; }
}

export default { CombineGraph }



